<?php

declare( strict_types = 1 );

namespace App\Common\Model\Base\DataUnit\JoinModelX;

use App\Common as com;
use App\Common\Model as nameModel;
use App\Common\Model\Base\DataUnit as nameDataUnit;
use App\Common\Model\Base\DataUnit\JoinModelX\ItemManage as thisItemManage;

class ItemManage extends nameDataUnit\Base {

	use com\Cla\ClaBase\Traits\GoPool;

	/**
	 * 示例：'id'
	 * @var string
	 */
	protected string $_joinConfig_TypeA;

	/**
	 * 预设置需要获取的类型方法(全部)
	 */
	protected array $_itemNamePoolAll = [// 'goXxx'
	];

	/**
	 * 需要获取的类型方法（默认为按需获取）
	 * @var array
	 */
	protected array $_itemNamePool = [];

	/**
	 * 示例：''
	 * @var string
	 */
	private string $_joinConfig_EchoSite;

	/**
	 * 设置为获取全部
	 * @return $this
	 */
	public function setToGetAll():self {
		$this->_itemNamePool = $this->_itemNamePoolAll;
		//
		return $this;
	}

	/**
	 * 获取池(ItemManage版)
	 * @param string $poolName 池名
	 * @param \Closure $callback 回调
	 * @return mixed
	 * @throws
	 */
	protected function goPoolOfItemManage( string $poolName, \Closure $callback ) {
		com\Lib\Fun\Array_::appendValueNx( $this->_itemNamePool, $poolName );
		return $this->goPool( $poolName, $callback );
	}

	/**
	 * 获取输出位置
	 * @return string
	 */
	public function getEchoSite():string {
		if( ! isset( $this->_joinConfig_EchoSite ) ){
			// 初始化输出位置
			$this->setEchoSite();
		}
		return $this->_joinConfig_EchoSite;
	}

	/**
	 * 设置输出位置
	 * @param string|null $echoSite 输出位置。[{null}]默认格式（joinXxx）
	 * @return static
	 */
	public function setEchoSite( string|null $echoSite = null ):static {
		if( $echoSite === null ){
			$this->_joinConfig_EchoSite = $this->getDefaultEchoSite();
		} else{
			$this->_joinConfig_EchoSite = $echoSite;
		}
		//
		return $this;
	}

	/**
	 * 获取默认EchoSite
	 * @return string
	 */
	private function getDefaultEchoSite():string {
		return 'join' . com\Lib\Fun\Class_::getClassBasename( static::class );
	}

	/**
	 * 检查是否为默认EchoSite
	 * @return bool
	 */
	public function isDefaultEchoSite():bool {
		return $this->_joinConfig_EchoSite === $this->getDefaultEchoSite();
	}

	/**
	 * @return void
	 * @throws com\Lib\Err\BaseException\Throw_
	 * @throws com\Lib\Err\Exception
	 */
	public function eventDataBefore() {
		if( ! isset( $this->_joinConfig_TypeA ) ){
			throw com\Lib\Err\Exception::makeOfNew( '', com\Lib\Err\Code::$sysDataError );
		}
		//
		/*
		if( ! empty( $this->getDataUnit()->goModelQuery()->getQuery()->columns ) ){
			$this->getDataUnit()->addField( $this->_joinConfig_TypeA, true );
		}
		*/
		$this->getDataUnit()->hasFieldOrAdd( $this->_joinConfig_TypeA, isRetain: false );
		//
		foreach( $this->_itemNamePool as $itemNameValue ){
			$this->$itemNameValue()->eventDataBeforeOfItem();
		}
	}

	/**
	 * @param $dataQuote
	 * @param $isMore
	 * @return void
	 */
	public function eventDataLater( &$dataQuote, $isMore = false ) {
		foreach( $this->_itemNamePool as $itemNameValue ){
			$itemObj = $this->$itemNameValue();
			//
			if( $this->getEchoSite() !== null ){
				if( $itemObj->isDefaultEchoSite() ){
					$itemObj->setEchoSite( $this->getEchoSite() );
				} else{
					$itemObj->setEchoSite( $this->getEchoSite() . '->' . $itemObj->getEchoSite() );
				}
			}
			//
			$itemObj->eventDataLaterOfItem( $dataQuote, $isMore );
		}
	}

	/**
	 * 全部执行ofApiPublic
	 * @return $this
	 */
	public function allCallAsGoFieldOfApiPublic():self {
		foreach( $this->_itemNamePoolAll as $itemNameValue ){
			/** @var nameModel\Base\DataUnit\JoinModelX\ItemManage\Item $itemObj */
			$itemObj = $this->$itemNameValue();
			$itemObj->goField()->ofApiPublic();
		}
		//
		return $this;
	}

	/**
	 * 初始化dataUnit参数
	 * @param com\Model\Base\DataUnit $dataUnit
	 * @return nameModel\Base\DataUnit
	 */
	/*
	protected function initJoinDataUnitParam( nameModel\Base\DataUnit $dataUnit ):nameModel\Base\DataUnit {
		if( $dataUnit->goOption()->getIsCleanIncompleteData() === null && $this->getDataUnit()->goOption()->getIsCleanIncompleteData() ){
			$dataUnit->goOption()->setIsCleanIncompleteData( true );
		}
		//
		return $dataUnit;
	}
	*/

}
